package pacBiopackage;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.Writer;

import tools.fasta.FastaSeq;
import tools.fasta.fastaParser;

public class PacBioMainAlignment {
	public static void main(String[] args) 
	{
		
		GetOpt go = new GetOpt(args, "o:p:s:a:ht:m:f:");
		go.optErr = true;
		int ch = -1;
		
		// process options in command line arguments
		// Set default values
		boolean usagePrint = false;                 
	//	String filenameMarker = "";                 
		String filenamePacBioreads = "";   
		String filenameOutput="output.txt";
		String filenameStat = "stat.txt";
		String filenameShore = "";
		int freq_mode=0;
		String freq_table="";
		int old_style = 0;
		while ((ch = go.getopt()) != GetOpt.optEOF) 
		{
		    if ((char)ch == 'h') {
		       usagePrint = true;
		   }
		    else if ((char)ch == 'p') {
		    	filenamePacBioreads = go.optArgGet();
		    }
		    else if((char)ch =='o')
		    {
		    	filenameOutput = go.optArgGet();
		    }
		    else if((char)ch =='s')
		    {
		    	filenameStat=go.optArgGet();
		    }
		    else if((char)ch =='a')
		    {
		    	filenameShore=go.optArgGet();
		    }
		    else if((char)ch =='t')
		    {
		    	old_style=Integer.valueOf(go.optArgGet());
		    }
		    else if((char)ch =='m')
		    {
		    	freq_mode=Integer.valueOf(go.optArgGet());
		    }
		    else if((char)ch == 'f')
		    {
		    	freq_table= go.optArgGet();
		    }
		    else
		        System.exit(1);                     // undefined option
		}                                           // getopt() returns '?'
		
		if (usagePrint || filenamePacBioreads.isEmpty() || filenameShore.isEmpty()) {
		    System.out.println("Usage: -p PacBio -a ShoreFile -t [ -o outputFile -s statistic -h]"); //NOI18N
		    System.exit(0);
		}
		
		// @TODO check if files exist
	
		System.out.println("------------------------");
		System.out.println("Command line parameter settings:");
		System.out.println("-p " + filenamePacBioreads); //NOI18N
		System.out.println("-o " + filenameOutput); //NOI18N
		System.out.println("-s " + filenameStat); //NOI18N
		System.out.println("-a " + filenameShore); //NOI18N
		System.out.println("------------------------");
		
		FastaSeq fs = null;
		fastaParser fp = null;
		PrintWriter pw = null;
					
		try 
		{
			Writer fw = new FileWriter(filenameOutput);
			Writer bw = new BufferedWriter(fw);
			pw = new PrintWriter(bw);
			PacBioWriter fileWriter = new PacBioWriter();
			
			fp = new fastaParser(new BufferedReader(new FileReader(filenamePacBioreads)));
			PacBioErrorStatisticsContainer stat = new PacBioErrorStatisticsContainer();
			
			CorretionbyAlignment alignment = null;
			if(old_style==0)
			{
				PacBioSNPLineReader pacBioReadShoreFile = new PacBioSNPLineReader(filenameShore);
				alignment = new PacBioCorrectionByAlignmentConsenus(pacBioReadShoreFile,stat);
			}
			else if(old_style==1)
			{
				PacBioSNPLineReader pacBioReadShoreFile = new PacBioSNPLineReader(filenameShore);
				alignment = new PacBioCorrectionByAlignment(pacBioReadShoreFile,stat); 
			}
			else if(old_style==2)
			{
				PacBioReadVCF pacBioReadVCFFile = new PacBioReadVCF(filenameShore);
				alignment = new PacBioCorrectionByAlignmentVcf(pacBioReadVCFFile, stat);
			}
			int counter=0;
			while(fp.hasNext())
			{
				fs=fp.next();
				System.out.println(counter);
//				if(counter == 1341)
//				{
//					System.out.println(fs.getHeader());
//					System.out.println(fs.getSeq());
//				}
//				System.out.println(counter);
				StringBuffer sb =alignment.correctByAlignment(fs,counter);
				System.out.println(counter+" done");
				fileWriter.writeToFile(sb, fs, pw);
				pw.println(alignment.getPositions().toString());
				pw.flush();
				++counter;
			}
			
			pw.close();
			pw = new PrintWriter(new BufferedWriter(new FileWriter(filenameStat))); //change to write the hole object!
			PacBioWriteStatistics stat_writer = new PacBioWriteStatistics();
			stat_writer.writeStats(stat, pw);
			pw.flush();
			pw.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}

	}
}
